package com.xxx.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.stereotype.Repository;

import com.xxx.common.dto.Pagination;
import com.xxx.common.utils.DateUtil;
import com.xxx.model.SysUserBasis;

@Repository
public class SysUserBasisDao {
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	/**
	 * 保存
	 * @param sysUserBasis
	 * @return id
	 */
	public long save(SysUserBasis sysUserBasis){
		String sql = "insert into sys_user_basis(id,logincode,idcard,username,phone,password,address,sex,status,recordstatus,createtime,updatetime) values (?,?,?,?,?,?,?,?,?,1,?,?)";
		Object obj[] = {sysUserBasis.getId(),sysUserBasis.getLogincode(),sysUserBasis.getIdcard(),sysUserBasis.getUsername(),sysUserBasis.getPhone(),sysUserBasis.getPassword(),sysUserBasis.getAddress(),sysUserBasis.getSex(),sysUserBasis.getStatus(),sysUserBasis.getCreatetime(),sysUserBasis.getUpdatetime()};
		int ret = jdbcTemplate.update(sql, obj);
		if (ret > 0) {
			return sysUserBasis.getId();
		}
		return 0;
	}
	
	/**
	 * 修改
	 * @param sysUserBasis
	 * @return id
	 */
	public long update(SysUserBasis sysUserBasis){
		String sql = "update sys_user_basis set logincode=?,idcard=?,username=?,phone=?,address=?,sex=?,status=?,updatetime=? where id=?";
		Object obj[] = {sysUserBasis.getLogincode(),sysUserBasis.getIdcard(),sysUserBasis.getUsername(),sysUserBasis.getPhone(),sysUserBasis.getAddress(),sysUserBasis.getSex(),sysUserBasis.getStatus(),sysUserBasis.getUpdatetime(), sysUserBasis.getId()};
		int ret = jdbcTemplate.update(sql, obj);
		if (ret > 0) {
			return sysUserBasis.getId();
		}
		return 0;
	}
	
	/**
	 * 逻辑删除
	 * @param id
	 * @return true/false
	 */
	public boolean delete(long id){
		String sql = "update sys_user_basis set recordstatus=0,updatetime=? where id=?";
		Object obj[] = {DateUtil.dateToLong(),id};
		int ret = jdbcTemplate.update(sql, obj);
		return ret > 0;
	}
	
	/**
	 * 重置密码
	 * @param id
	 * @param pwd
	 * @return
	 */
	public boolean resetPwd(long id,String pwd){
		String sql = "update sys_user_basis set password=?,updatetime=? where id=?";
		Object obj[] = {pwd,DateUtil.dateToLong(),id};
		int ret = jdbcTemplate.update(sql, obj);
		return ret > 0;
	}
	
	/**
	 * 获取一个
	 * @param id
	 * @return SysUserBasis
	 */	
	public SysUserBasis getEntity(long id){
		String sql = "select id,logincode,idcard,username,phone,password,address,sex,status,createtime,updatetime from sys_user_basis where recordstatus=1 and id=?";
		final SysUserBasis sysUserBasis = new SysUserBasis();
		jdbcTemplate.query(sql, new Object[]{id},new RowCallbackHandler(){
			public void processRow(ResultSet rs) throws SQLException {
				getResultSet(rs, sysUserBasis);
            }
		});
		return sysUserBasis;
	}
	
	/**
	 * 根据LoginCode获取
	 * @param id
	 * @return SysUserBasis
	 */	
	public SysUserBasis getEntityByLoginCode(String logincode){
		String sql = "select id,logincode,idcard,username,phone,password,address,sex,status,createtime,updatetime from sys_user_basis where recordstatus=1 and logincode=? limit 1";
		final SysUserBasis sysUserBasis = new SysUserBasis();
		jdbcTemplate.query(sql, new Object[]{logincode},new RowCallbackHandler(){
			public void processRow(ResultSet rs) throws SQLException {
				getResultSet(rs, sysUserBasis);
			}
		});
		return sysUserBasis;
	}
	
	/**
	 * 根据LoginCode获取
	 * @param id
	 * @return SysUserBasis
	 */	
	public SysUserBasis getEntityByPhone(String phone){
		String sql = "select id,logincode,idcard,username,phone,password,address,sex,status,createtime,updatetime from sys_user_basis where recordstatus=1 and phone=? limit 1";
		final SysUserBasis sysUserBasis = new SysUserBasis();
		jdbcTemplate.query(sql, new Object[]{phone},new RowCallbackHandler(){
			public void processRow(ResultSet rs) throws SQLException {
				getResultSet(rs, sysUserBasis);
			}
		});
		return sysUserBasis;
	}

	/**
	 * 获取list
	 * @param whereMap
	 * @return list
	 */		
	public List<SysUserBasis> getList(Map<String, Object> whereMap){
		final List<SysUserBasis> list = new ArrayList<>();
		StringBuffer sql = new StringBuffer("select id,logincode,idcard,username,phone,password,address,sex,status,createtime,updatetime from sys_user_basis where recordstatus=1 ");
		List<Object> whereObj = new ArrayList<>();
		sql = getCondition(whereMap, sql, whereObj);
		Object obj[] = whereObj.toArray(new Object[whereObj.size()]);
		jdbcTemplate.query(sql.toString(), obj ,new RowCallbackHandler(){
			public void processRow(ResultSet rs) throws SQLException {
				list.add(getResultSet(rs, new SysUserBasis()));
            }
		});
		return list;
	}

	/**
	 * 获取list,分页
	 * @param whereMap
	 * @param pagination
	 * @return list
	 */			
	public List<SysUserBasis> getList(Map<String, Object> whereMap,Pagination pagination){
		final List<SysUserBasis> list = new ArrayList<>();
		StringBuffer sql = new StringBuffer("select id,logincode,idcard,username,phone,password,address,sex,status,createtime,updatetime from sys_user_basis where recordstatus=1 ");
		List<Object> whereObj = new ArrayList<>();
		sql = getCondition(whereMap, sql, whereObj);
		Object obj[] = whereObj.toArray(new Object[whereObj.size()]);
		sql.append(" order by " + pagination.getOrder());
		sql.append(" limit " + pagination.getPage() + "," + pagination.getPageSize());
		jdbcTemplate.query(sql.toString(), obj ,new RowCallbackHandler(){
			public void processRow(ResultSet rs) throws SQLException {
				list.add(getResultSet(rs, new SysUserBasis()));
			}
		});
		return list;
	}

	/**
	 * 获取个数
	 * @param whereMap
	 * @return int
	 */		
	public int getCount(Map<String, Object> whereMap){
		StringBuffer sql = new StringBuffer("select count(1) from sys_user_basis where recordstatus=1");
		List<Object> whereObj = new ArrayList<>();
		sql = getCondition(whereMap, sql, whereObj);
		Object obj[] = whereObj.toArray(new Object[whereObj.size()]);
		Integer count = jdbcTemplate.queryForObject(sql.toString(), obj, Integer.class);
		if(count != null){
			return count;
		}
		return 0;
	}
	
	/**
	 * 获取条件
	 * @param whereMap
	 * @param sql
	 * @param whereObj
	 * @return sql
	 */	
	private StringBuffer getCondition(Map<String, Object> whereMap,StringBuffer sql,List<Object> whereObj){
		if(whereMap == null){
			return sql;
		}
		if (whereMap.containsKey("logincode")) {
			String logincode = (String) whereMap.get("logincode");
			sql.append(" and logincode=?");
			whereObj.add(logincode);
		}
		if (whereMap.containsKey("username")) {
			String username = (String) whereMap.get("username");
			sql.append(" and username like ?");
			whereObj.add(username + "%");
		}
		if (whereMap.containsKey("idcard")) {
			String idcard = (String) whereMap.get("idcard");
			sql.append(" and idcard=?");
			whereObj.add(idcard);
		}
		if (whereMap.containsKey("phone")) {
			String phone = (String) whereMap.get("phone");
			sql.append(" and phone=?");
			whereObj.add(phone);
		}
		if (whereMap.containsKey("status")) {
			int status = (int) whereMap.get("status");
			sql.append(" and status=?");
			whereObj.add(status);
		}
		return sql;
	}
	
	/**
	 * 获取结果集
	 * @param whereMap
	 * @param sysUserBasis
	 * @return sysUserBasis
	 */	
	private SysUserBasis getResultSet(ResultSet rs,SysUserBasis sysUserBasis) throws SQLException{
		sysUserBasis.setId(rs.getLong("id"));
		sysUserBasis.setLogincode(rs.getString("logincode"));
		sysUserBasis.setIdcard(rs.getString("idcard"));
		sysUserBasis.setUsername(rs.getString("username"));
		sysUserBasis.setPhone(rs.getString("phone"));
		sysUserBasis.setPassword(rs.getString("password"));
		sysUserBasis.setAddress(rs.getString("address"));
		sysUserBasis.setSex(rs.getInt("sex"));
		sysUserBasis.setStatus(rs.getInt("status"));
		sysUserBasis.setCreatetime(rs.getLong("createtime"));
		sysUserBasis.setUpdatetime(rs.getLong("updatetime"));
		return sysUserBasis;
	}
}